
package com.sausageking.client.consumer.ui;

import com.google.common.base.Strings;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.sausageking.client.ConsumerService.AuthType;
import com.sausageking.client.common.Context;
import com.sausageking.shared.BuyerException.Code;

public class RedeemDialogViewImpl extends Composite implements RedeemDialogView {

  interface ViewUiBinder extends UiBinder<Widget, RedeemDialogViewImpl> {
  }

  private static ViewUiBinder uiBinder = GWT.create(ViewUiBinder.class);

  private Presenter presenter;

  // private SimplePanel glass;

  private DialogBox dialogBox;

  @UiField
  Label errorLabel;

  @UiField
  PasswordTextBox passwordTextBox;

  @UiField
  Label passwordLabel;

  @UiField
  VerticalPanel authSection;

  @UiField
  Button redeemButton;

  private Context context;

  @Inject
  public RedeemDialogViewImpl() {
    initWidget(uiBinder.createAndBindUi(this));
    initGlass();
    initDialog();
    passwordTextBox.addKeyPressHandler(new KeyPressHandler() {
      @Override
      public void onKeyPress(KeyPressEvent event) {
        if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
          onRedeemClicked(null);
        }
      }
    });
  }

  @UiHandler("redeemButton")
  void onRedeemClicked(ClickEvent e) {
    if (validate()) {
      presenter.redeem(context.getEmail(), context.getMerchant(),
          AuthType.PASSWORD, passwordTextBox.getText());
    }
  }

  private boolean validate() {
    if (Strings.isNullOrEmpty(passwordTextBox.getText())) {
      setError(Code.EMPTY_PASSWORD);
      return false;
    } else if (passwordTextBox.getText().length() < 4) {
      setError(Code.INVALID_PASSWORD_LENGTH);
      return false;
    }
    return true;
  }

  @UiHandler("cancelButton")
  void onCancelClicked(ClickEvent e) {
    hide();
    clear();
  }

  @Override
  public RedeemDialogView setContext(Context context) {
    this.context = context;
    if (context.hasPassword()) {
      passwordLabel.setText("Enter Your PIN");
      redeemButton.setText("Redeem");
    } else {
      passwordLabel.setText("Pick Your PIN");
      redeemButton.setText("Redeem");
    }
    return this;
  }

  @Override
  public void show() {
    // glass.setVisible(true);
    dialogBox.setGlassEnabled(true);
    dialogBox.center();
  }

  @Override
  public void hide() {
    clear();
    // glass.setVisible(false);
    dialogBox.setGlassEnabled(false);
    dialogBox.hide();
  }

  private void clear() {
    passwordTextBox.setText("");
    errorLabel.setText("");
    errorLabel.setVisible(false);
  }

  private void initGlass() {
    // if (glass == null) {
    // glass = new SimplePanel();
    // glass.setStyleName("popupglass");
    // glass.setHeight(Window.getClientHeight() + "px");
    // RootPanel.get().add(glass);
    // }
    // glass.setVisible(false);
  }

  private void initDialog() {
    if (dialogBox == null) {
      dialogBox = new DialogBox();
      dialogBox.setStyleName("dialogBox");
      dialogBox.add(this);
      dialogBox.hide();
    }
  }

  @Override
  public RedeemDialogView setError(Code errorCode) {
    clear();
    errorLabel.setText(errorCode.getMessage());
    errorLabel.setVisible(true);
    passwordTextBox.setFocus(true);
    return this;
  }

  @Override
  public RedeemDialogView setPresenter(Presenter presenter) {
    this.presenter = presenter;
    return this;
  }
}
